package org.jeecg.modules.wlhy.customerwallet.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.paycenter.service.WalletService;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customerwallet.entity.HyCustomerWallet;
import org.jeecg.modules.wlhy.customerwallet.service.IHyCustomerWalletService;
import org.jeecg.modules.wlhy.hyproject.service.IHyProjectService;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Arrays;

/**
 * @Description: hy_customer_offline_wallet
 * @Author: jeecg-boot
 * @Date:   2021-10-20
 * @Version: V1.0
 */
@Api(tags="hy_customer_offline_wallet")
@RestController
@RequestMapping("/customerwallet/hyCustomerOfflineWallet")
@Slf4j
public class HyCustomerOfflineWalletController extends JeecgController<HyCustomerWallet, IHyCustomerWalletService> {
	@Autowired private IHyCustomerWalletService hyCustomerWalletService;
	@Autowired private WalletService walletService;
	@Autowired private UserUtil userUtil;
	@Autowired
	private IHyProjectService hyProjectService;



	@ApiOperation(value="hy_customer_offline_wallet-分页列表查询", notes="hy_customer_offline_wallet-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HyCustomerWallet hyCustomerOfflineWallet,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HyCustomerWallet> queryWrapper = QueryGenerator.initQueryWrapper(hyCustomerOfflineWallet, req.getParameterMap());



		Page<HyCustomerWallet> page = new Page<HyCustomerWallet>(pageNo, pageSize);
		IPage<HyCustomerWallet> pageList = hyCustomerWalletService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @GetMapping(value = "/getCustomerOfflineWalletInfo")
	 public Result<?> getCustomerOfflineWalletInfo() {
		 HyCustomer customer = userUtil.getCustomer();
		 if(customer==null){
			 return Result.error("货主信息不存在！");
		 }
		 HyCustomerWallet wallet  = hyCustomerWalletService.getByCustomerId(customer.getId());
		 if(wallet != null){
		 	//计算待支付运费
			 BigDecimal djsAmt = hyCustomerWalletService.getDJSByCustomerId(customer.getId());
			 JSONObject jo = (JSONObject) JSONObject.toJSON(wallet);
			 jo.put("djsAmt",djsAmt);
			 return Result.OK(jo);
		 }
		 return Result.OK(null);
	 }

	@AutoLog(value = "hy_customer_offline_wallet-添加")
	@ApiOperation(value="hy_customer_offline_wallet-添加", notes="hy_customer_offline_wallet-添加")
	@PostMapping(value = "/add")
	@RequiresPermissions("wallet:add")
	public Result<?> add(@RequestBody HyCustomerWallet hyCustomerOfflineWallet) {
		String customerId = hyCustomerOfflineWallet.getCustomerId();
		if(StringUtils.isEmpty(customerId)){
			return Result.error("货主不能为空！");
		}
		HyCustomerWallet wallet  = hyCustomerWalletService.getByCustomerId(customerId);
		if(wallet !=null){
			return Result.error("不能重复添加！");
		}
		hyCustomerOfflineWallet.setAmt(BigDecimal.ZERO);
		hyCustomerWalletService.save(hyCustomerOfflineWallet);
		return Result.OK("添加成功！");
	}

	@AutoLog(value = "hy_customer_offline_wallet-编辑")
	@ApiOperation(value="hy_customer_offline_wallet-编辑", notes="hy_customer_offline_wallet-编辑")
	@PutMapping(value = "/edit")
	@RequiresPermissions("wallet:add")
	public Result<?> edit(@RequestBody HyCustomerWallet hyCustomerOfflineWallet) {

		String customerId = hyCustomerOfflineWallet.getCustomerId();
		if(StringUtils.isEmpty(customerId)){
			return Result.error("货主不能为空！");
		}
		HyCustomerWallet wallet  = hyCustomerWalletService.getByCustomerId(customerId);
		if(wallet ==null){
			return Result.error("信息不存在！");
		}
		wallet.setSt(hyCustomerOfflineWallet.getSt());
		hyCustomerWalletService.updateById(wallet);
		return Result.OK("编辑成功!");
	}

	 @AutoLog(value = "hy_customer_offline_wallet-充值")
	 @ApiOperation(value="hy_customer_offline_wallet-充值", notes="hy_customer_offline_wallet-充值")
	 @PutMapping(value = "/recharge")
	 @RequiresPermissions("wallet:recharge")
	 public Result<?> recharge(@RequestBody JSONObject jsonObject) {
		 String remark = jsonObject.getString("remark");
		 String customerId = jsonObject.getString("customerId");
		 BigDecimal amt = jsonObject.getBigDecimal("amt");
		 if(StringUtils.isEmpty(customerId)){
			 return Result.error("货主不能为空！");
		 }
		 if(amt == null){
			 return Result.error("充值金额不正确！");
		 }
		 if(amt.compareTo(BigDecimal.ZERO) < 1){
			 return Result.error("金额不正确！");
		 }
		 walletService.recharge(customerId,amt,remark);
		 return Result.OK("充值成功!");
	 }

	 @AutoLog(value = "hy_customer_offline_wallet-扣除")
	 @ApiOperation(value="hy_customer_offline_wallet-扣除", notes="hy_customer_offline_wallet-扣除")
	 @PutMapping(value = "/kc")
	 @RequiresPermissions("wallet:recharge")
	 public Result<?> kc(@RequestBody JSONObject jsonObject) {
		 String remark = jsonObject.getString("remark");
		 String customerId = jsonObject.getString("customerId");
		 BigDecimal amt = jsonObject.getBigDecimal("amt");
		 if(StringUtils.isEmpty(customerId)){
			 return Result.error("货主不能为空！");
		 }
		 if(amt == null){
			 return Result.error("金额不正确！");
		 }
		 //<=0
		 if(amt.compareTo(BigDecimal.ZERO) < 1){
			 return Result.error("金额不正确！");
		 }
		 walletService.kc(customerId,amt,remark);
		 return Result.OK("充值成功!");
	 }

}
